package ru.reaperoq.inventory.screens.main

import LoadingStatus
import UserViewModel
import androidx.activity.ComponentActivity
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import models.shared.UserUi
import my.models.ComponentUi
import my.models.ComputerUi
import org.koin.androidx.compose.navigation.koinNavViewModel
import ru.reaperoq.inventory.components.button.BigButton
import ru.reaperoq.inventory.components.text.TextWithShadow
import ru.reaperoq.inventory.loadingColors
import ru.reaperoq.inventory.unverifiedColors
import ru.reaperoq.inventory.verificationShimmer
import ru.reaperoq.inventory.verifiedColors
import verification.VerificationViewModel
import verification.models.VerificationEvent

@Composable
fun VerificationScreen(
    modifier: Modifier = Modifier,
    viewModel: VerificationViewModel = koinNavViewModel(),
    userViewModel: UserViewModel = koinNavViewModel(
        key = "USER_VIEW_MODEL",
        viewModelStoreOwner = (LocalContext.current as ComponentActivity)
    ),
    serial: String,
    onEditClick: (Int) -> Unit,
    onBack: () -> Unit
) {
    val viewState by viewModel.viewStates().collectAsState()
    val userState by userViewModel.viewStates().collectAsState()

    LaunchedEffect(serial) {
        viewModel.obtainEvent(VerificationEvent.Verify(serial))
    }

    AnimatedContent(
        targetState = viewState.loadingStatus,
        label = "VerificationScreen loading"
    ) {
        when (it) {
            is LoadingStatus.Error -> {
                AnimatedContent(
                    targetState = viewState.isVerified,
                    label = "VerificationScreen verified"
                ) { v ->
                    VerifiedScreen(
                        modifier,
                        userState.user?.role,
                        v,
                        obj = viewState.verifiedObject,
                        onEditClick,
                        onBack
                    )
                }
            }

            LoadingStatus.FullyLoad -> {
                AnimatedContent(
                    targetState = viewState.isVerified,
                    label = "VerificationScreen verified"
                ) { v ->
                    VerifiedScreen(
                        modifier,
                        userState.user?.role,
                        v,
                        obj = viewState.verifiedObject,
                        onEditClick,
                        onBack
                    )
                }
            }

            else -> {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .verificationShimmer(loadingColors),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "Проверка...", style = MaterialTheme.typography.titleLarge.copy(
                            fontWeight = FontWeight.Bold
                        )
                    )
                }
            }
        }

    }
}

@Composable
fun VerifiedScreen(
    modifier: Modifier = Modifier,
    role: UserUi.Role?,
    isVerified: Boolean = true,
    obj: Any?,
    onEditClick: (Int) -> Unit,
    onBack: () -> Unit
) {
    val colors = if (isVerified) verifiedColors else unverifiedColors
    Box(
        modifier = modifier
            .fillMaxSize()
            .verificationShimmer(colors),
        contentAlignment = Alignment.Center
    ) {
        CompositionLocalProvider(
            LocalTextStyle provides MaterialTheme.typography.titleLarge.copy(
                fontWeight = FontWeight.Bold,
                color = Color.White,
                textAlign = TextAlign.Center
            )
        ) {
            when (obj) {
                null -> TextWithShadow(
                    modifier = Modifier.align(Alignment.Center),
                    text = "Объект не найден"
                )

                is ComputerUi -> TextWithShadow(
                    modifier = Modifier.align(Alignment.Center),
                    text = "${obj.manufacturer} ${obj.model}"
                )

                is ComponentUi -> TextWithShadow(
                    modifier = Modifier.align(Alignment.Center),
                    text = "${obj.type} ${obj.name}"
                )

                else -> TextWithShadow(
                    modifier = Modifier.align(Alignment.Center),
                    text = "Неизвестный объект"
                )
            }
        }
        Column(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            if (role != UserUi.Role.USER) {
                BigButton(
                    onClick = {
                        onEditClick(
                            when (obj) {
                                is ComputerUi -> obj.id
                                is ComponentUi -> obj.id
                                else -> return@BigButton
                            }
                        )
                    }
                ) {
                    Icon(imageVector = Icons.Default.Edit, contentDescription = null)
                    Text(text = "Редактировать")
                }
            }
            BigButton(
                modifier = Modifier
                    .navigationBarsPadding()
                    .padding(bottom = 16.dp),
                onClick = onBack
            ) {
                Icon(imageVector = Icons.AutoMirrored.Default.ArrowBack, contentDescription = null)
                Text(text = "Назад")
            }
        }
    }
}